package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.Custom;
import com.situ.springboot.pojo.vo.PasswordVo;
import com.situ.springboot.service.CustomService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/custom")
public class CustomController {

    @Autowired
    private CustomService customService;

    @RequestMapping("/selectall")  //查询客户所有信息
    public String selectAll(Model model){
        List<Custom> list=customService.selectall();
        model.addAttribute("list",list);
        return "custom/list";
    }

    @RequestMapping("/toadd")
    public String toadd(){
        return "custom/add";
    }

    @RequestMapping("/add")  //添加客户信息
    public String add(Custom custom){
        if(custom.getMoney()==null) custom.setMoney(0.0);
        if(custom.getPassword()==null || Objects.equals(custom.getPassword(), "")){
            custom.setPassword("123456");
        }
        customService.add(custom);
        return "redirect:/custom/selectByPage";
    }

    @RequestMapping("/deleteById")  //根据id删除管理员
    public String deleteById(Integer id) {
        customService.deleteById(id);
        return "redirect:/custom/selectByPage";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        customService.deleteAll(ids);
        return "redirect:/custom/selectByPage";
    }

    @RequestMapping("/toupdate")  //根据id找到需要更新的数据转发到admin/update
    public String selectById(Integer id, Model model){
        Custom custom = customService.selectById(id);
        model.addAttribute("custom",custom);
        return "custom/update";
    }
    @RequestMapping("/update")  //更新数据
    public String update(Custom custom){
        customService.update(custom);
        return "redirect:/custom/selectByPage";
    }

    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("UserController.selectByPage");
        PageInfo pageInfo = customService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到user_list界面展示
        return "/custom/list";
    }


    ///////////////////////// 当前登录客户 的 个人信息展示
    @RequestMapping("/toCustomInformation")
    public String showCustomInformation(HttpSession session, Model model){
        Custom custom1 = (Custom) session.getAttribute("custom");
        Integer id = custom1.getId();
        Custom custom = customService.selectById(id);
        model.addAttribute("custom",custom);

        System.out.println("当前登录用户的信息:" + custom);

        return "/custom/custom_information";
    }

    @RequestMapping("/UpdatePassword")
    public String UpdatePassword(HttpSession session, Model model, PasswordVo passwordVo){
        Custom custom1 = (Custom) session.getAttribute("custom");
        Integer id = custom1.getId();
        Custom custom = customService.selectById(id);
        if(Objects.equals(passwordVo.getPassword(), "")
                || Objects.equals(passwordVo.getPassword1(), "")
                || Objects.equals(passwordVo.getPassword2(), "")){
            return "index_custom";
        }
        else {
            custom.setPassword(passwordVo.getPassword1());
            customService.update(custom);
            return "redirect:/user/logout";
        }
    }

    ///////////////////////充值
    @RequestMapping("/toIncreaseMoney")
    public String toIncreaseMoney(){
        return "/custom/vip";
    }

}
